<?php
namespace app\api\service\ceping;

// ====================== 核心算法 ======================
class KaoshixinliService {
    private $weights = [
        '考试焦虑' => 1.3,
        '复习计划性' => 1.1,
        '时间管理' => 1.0,
        '自信心' => 1.2,
        '应对策略' => 0.9,
        '身体状态' => 1.0
    ];
	//采用李克特5级量表（1=完全不符合，5=完全符合）：
	// ====================== 题库配置 ======================
	public $questionBank = [
		'考试焦虑' => [
			['id' => 1, 'text' => '我经常在考试前感到紧张或心慌', 'type' => '正向', 'weight' => 1.2],
			['id' => 2, 'text' => '考试时我会因过度紧张而忘记原本会的内容', 'type' => '正向', 'weight' => 1.1],
			['id' => 3, 'text' => '考试前我容易失眠或食欲下降', 'type' => '正向', 'weight' => 1.1]
		],
		'复习计划性' => [
			['id' => 4, 'text' => '我会提前制定详细的复习计划并按计划执行', 'type' => '反向', 'weight' => 1.0],
			['id' => 5, 'text' => '我清楚知道自己的知识薄弱点并有针对性地复习', 'type' => '反向', 'weight' => 1.0],
			['id' => 6, 'text' => '我通常在考试前临时抱佛脚', 'type' => '正向', 'weight' => 1.0]
		],
		'时间管理' => [
			['id' => 7, 'text' => '我能在复习时保持专注，不受手机等干扰', 'type' => '反向', 'weight' => 1.1],
			['id' => 8, 'text' => '我经常因拖延导致复习时间不足', 'type' => '正向', 'weight' => 1.2],
			['id' => 9, 'text' => '我会合理分配不同科目的复习时间', 'type' => '反向', 'weight' => 1.2]
		],
		'自信心' => [
			['id' => 10, 'text' => '我对自己的考试准备感到充分和自信', 'type' => '反向', 'weight' => 1.1],
			['id' => 11, 'text' => '我担心自己的成绩不如他人', 'type' => '正向', 'weight' => 1.2],
			['id' => 12, 'text' => '我相信即使遇到难题也能冷静应对', 'type' => '反向', 'weight' => 1.2]
		],
		'应对策略' => [
			['id' => 13, 'text' => '遇到难题时我会先跳过，最后再回来解决', 'type' => '反向', 'weight' => 1.1],
			['id' => 14, 'text' => '我习惯在考试中检查答案以避免粗心错误', 'type' => '反向', 'weight' => 1.2],
			['id' => 15, 'text' => '考试后我会分析错题并总结改进方法', 'type' => '反向', 'weight' => 1.2]
		],
		'身体状态' => [
			['id' => 16, 'text' => '考试前我会保持规律作息和适度运动', 'type' => '反向', 'weight' => 1.1],
			['id' => 17, 'text' => '我经常因疲劳复习导致效率低下', 'type' => '正向', 'weight' => 1.2],
			['id' => 18, 'text' => '考试期间我能保持合理的饮食和水分摄入', 'type' => '反向', 'weight' => 1.2]
		]
	];
    // 生成测评试卷
    public function generateTest($numPerDimension = 2) {
        $test = [];
        foreach ($this->weights as $dim => $weight) {
			//var_dump($questionBank);var_dump($questionBank[$dim]);die;
            shuffle($this->questionBank[$dim]);
            $test = array_merge($test, array_slice($this->questionBank[$dim], 0, $numPerDimension));
        }
        return $test;
    }

    // 计算得分
    public function calculateScore($answers) {
        $dimensionScores = [];
        $totalScore = 0;
        $maxPossible = 0;

        foreach ($answers as $qId => $response) {
            foreach ($this->questionBank as $dim => $questions) {
                foreach ($questions as $q) {
                    if ($q['id'] == $qId) {
                        // 计算有效分数
                        $effectiveScore = ($q['type'] == '正向') ? $response : 6 - $response;
                        $weightedScore = $effectiveScore * $q['weight'];
                        
                        $dimensionScores[$dim] = ($dimensionScores[$dim] ?? 0) + $weightedScore;
                        $totalScore += $weightedScore;
                        $maxPossible += 5 * $q['weight'];
                        break 2;
                    }
                }
            }
        }

        // 计算风险等级
        $riskRatio = $totalScore / $maxPossible;
        $riskLevel = $this->determineRiskLevel($riskRatio);
		//建议
		if ($riskLevel == '高风险') {
			$suggestion = ["建议进行专业心理咨询，优先处理考试焦虑问题","每日进行深呼吸练习（早中晚各3分钟）"];
			
		} elseif ($riskLevel == '中风险') {
			$suggestion = ["推荐使用番茄工作法改善时间管理","每周进行2次模拟考试训练"];
		} else {
			$suggestion = "保持良好的备考习惯，注意劳逸结合";
		}
        return [
            'score' => $totalScore,
            'level' => $riskLevel,
            'suggestion' => $suggestion,
            'dimensions' => $dimensionScores,
            //'max_possible' => $maxPossible
        ];
    }

    private function determineRiskLevel($ratio) {
        if ($ratio < 0.3) return '低风险';
        if ($ratio < 0.6) return '中风险';
        return '高风险';
    }
}
?>